package com.community.controller;

import org.springframework.transaction.annotation.Transactional;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.community.pojo.Consumer;
import com.community.service.IConsumerService;
import com.community.utils.PageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author TangHaoSong Fly ChenJie HeYuHang
 * @since 2024-05-15
 */
@CrossOrigin
@RestController
@RequestMapping("/community/consumer")
public class ConsumerController {
    @Autowired
    private IConsumerService iConsumerService;

    /**
     * 图片上传
     *
     * @param photo
     * @return
     */
    @PostMapping("/upload")
    public String upload(MultipartFile photo) {
        System.out.print(photo);
        if (photo == null) {
            return "error";
        }
        File file = new File("D:\\pictures");
        if (!file.exists()) {
            file.mkdirs();
        }
        String suffix = photo.getOriginalFilename().substring(photo.getOriginalFilename().lastIndexOf("."));
        String filename = UUID.randomUUID() + "_" + suffix;
        //写文件
        try {
            photo.transferTo(new File(file, filename));
        } catch (IOException e) {
            e.printStackTrace();
        }
        String result = "http://localhost:10001/" + filename;
        return result;
    }

    /**
     * 登录
     *
     * @param consumer
     * @return
     */
    @RequestMapping("/login")
    public String login(@RequestBody Consumer consumer) {
        QueryWrapper<Consumer> wrapper = new QueryWrapper<>();
        wrapper.eq("login_name", consumer.getLoginName());
        wrapper.eq("password", consumer.getPassword());
        Consumer one = iConsumerService.getOne(wrapper);
        if (one == null) {
            return "error";
        }
        return consumer.getLoginName();
    }

    /**
     * 注册
     *
     * @param consumer
     * @return
     */
    @RequestMapping("/register")
    @Transactional
    public String register(@RequestBody Consumer consumer) {

        Consumer consumer1 = new Consumer();

            //前台用户id
            consumer1.setConsumerId(null);
            consumer1.setLoginName(consumer.getLoginName());
            //登录密码
            consumer1.setPassword(consumer.getPassword());
            //手机号码
            consumer1.setPhone(consumer.getPhone());
            //账户余额
            consumer1.setBalance(new Double(0));
            //头像
            consumer1.setAvatarImg(consumer.getAvatarImg());
            //性别
            consumer1.setSex(consumer.getSex());
            //创建时间
            consumer1.setCreateDate(new Date());
            //是否为商户,S001-1（普通用户），S001-4（商户)
            consumer1.setMerchantTag("S001-1");
            //是否是供货商，S001-1（普通用户），S001-4（供货商)
            consumer1.setSupplierTag("S001-1");
            //是否删除,C001-1（未删除）、C001-2（已删除)
            consumer1.setStatus("C001-1");
            this.iConsumerService.save(consumer1);

        return "ok";
    }


    @RequestMapping("/add")
    @ResponseBody
    public String add(@RequestBody Consumer info) {
        boolean flag = this.iConsumerService.save(info);
        if (flag) {
            return "ok";
        }
        return "error";
    }

    /**
     * 删除
     *
     * @param consumer
     * @return
     */
    @RequestMapping("/delete")
    @ResponseBody
    public String delConsumer(@RequestBody Consumer consumer) {
        iConsumerService.delConsumer(consumer);
        return "删除成功！";
    }

    /**
     * 批量删除用户
     *
     * @param consumerList
     * @return
     */
    @RequestMapping("/delsConsumer")
    @ResponseBody
    public String delsConsumer(@RequestBody List<Consumer> consumerList) {
        iConsumerService.delsConsumer(consumerList);
        return "删除成功！";
    }

    /**
     * 用户冻结/正常
     *
     * @param consumer
     * @return
     */
    @RequestMapping("/switchState")
    public String switchState(@RequestBody Consumer consumer) {
        iConsumerService.switchState(consumer);
        return "冻结成功";
    }

        /**
         * 用户列表查询
         *
         * @param pageNo    当前页码
         * @param pageSize  每页显示的记录数
         * @param consumers 查询条件
         * @return 分页结果
         */
        @RequestMapping("/getConsumerList/{pageNo}/{pageSize}")
        public PageUtils<Consumer> page ( @PathVariable int pageNo,
        @PathVariable int pageSize,
        @RequestBody Consumer consumers){
            // 调用 sysUsersService 的 getSysUsersList 方法查询用户列表
            PageUtils<Consumer> pageInfo = this.iConsumerService.getConsumersList(consumers, pageNo, pageSize);
            System.out.println("pageInfo = " + pageInfo);
            return pageInfo;
        }

        /**
         * 商户根据用户表id查询对应的商户信息
         *
         * @param id
         * @return
         */
        @RequestMapping("/merchantById/{id}")
        @ResponseBody
        public Consumer merchantById ( @PathVariable int id){
            Consumer goodsInfo = this.iConsumerService.getById(id);
            System.out.println("1111111 = " + goodsInfo);
            goodsInfo.setMerchantTag("S001-4");
            this.iConsumerService.updateById(goodsInfo);
            return goodsInfo;
        }
        /**
         * 商户根据用户表id查询对应的商户信息
         * @param id
         * @return
         */
        @RequestMapping("/supplierById/{id}")
        @ResponseBody
        public Consumer supplierById ( @PathVariable int id){
            Consumer goodsInfo = this.iConsumerService.getById(id);
            System.out.println("1111111 = " + goodsInfo);
            goodsInfo.setSupplierTag("G001-4");
            this.iConsumerService.updateById(goodsInfo);
            return goodsInfo;
       }
    }


